package com.xyy.saas.payment.callback.core.job;

import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.xyy.elasticjob.client.annotation.ElasticJobConf;
import com.xyy.saas.payment.adpater.RouteManager;
import com.xyy.saas.payment.callback.core.result.CallbackRunner;
import com.xyy.saas.payment.merchant.core.dto.WithdrawRefundDto;
import com.xyy.saas.payment.merchant.core.dto.WithdrawRefundVo;
import com.xyy.saas.payment.util.DateUtil;
import com.xyy.saas.payment.util.JSONUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import java.util.Date;

/**
 * 同步小额鉴权退汇状态
 */
@Slf4j
@ElasticJobConf(name = "com.xyy.saas.payment.callback.core.job.SyncSmallAmountRefundStatusJob", cron = "0 0 0/1 * * ? ")
public class SyncSmallAmountRefundStatusJob implements SimpleJob {
    @Resource
    private RouteManager routeManager;
    @Resource
    private CallbackRunner callbackRunner;

    @Override
    public void execute(ShardingContext shardingContext) {
        log.info("SyncSmallAmountRefundStatusJob#execute start.");
        try {
            Date endDate = DateUtil.now();
            Date startDate = DateUtil.getOtherDay(endDate, -2);
            WithdrawRefundVo withdrawRefundVo = routeManager.withdrawRefund(WithdrawRefundDto.builder().
                    refundType(WithdrawRefundDto.SMALL_AMOUNT).
                    startDate(startDate).
                    endDate(endDate).build());
            log.error("SyncSmallAmountRefundStatusJob#execute withdrawRefundVo={}", JSONUtils.toJSON(withdrawRefundVo));

            if(withdrawRefundVo == null || CollectionUtils.isEmpty(withdrawRefundVo.getWithdrawRefundItems())) {
                return;
            }
        }
        catch(Exception ex) {
            log.error(ex.getMessage(), ex);
        }
    }
}
